package bev.frames.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JOptionPane;

import bev.HelperUtility;
import bev.article.BaseArticle;

public class ArticlePane extends JPanel {
    private static final long serialVersionUID = -6509578546742072124L;

    ClientFrame parentFrame;
    BaseArticle article;
    boolean inEditMode = false;

    // Components and controls.
    JPanel articleCtrls;
    GridBagConstraints gbcTxtPane;
    JScrollPane artTextPane;
    JButton btnEdit;
    JButton btnCancel;

    public void setEditMode( boolean editMode ) {
        btnCancel.setVisible( editMode );
        btnEdit.setText( editMode ? "Save" : "Edit" );
        inEditMode = editMode;
    }

    public boolean getEditMode() {
        return inEditMode;
    }

    // An articlePanel used to display the article
    // currently being viewed by the user.
    public ArticlePane( ClientFrame parent ) {
        this.parentFrame = parent;

        setLayout( new GridBagLayout() );

        // Article controls.
        GridBagConstraints gbcArtCtrls = new GridBagConstraints();
        gbcArtCtrls.fill = GridBagConstraints.HORIZONTAL;
        gbcArtCtrls.anchor = GridBagConstraints.NORTH;
        gbcArtCtrls.gridx = 0;
        gbcArtCtrls.gridy = 0;
        gbcArtCtrls.weightx = 1.0;
        gbcArtCtrls.weighty = 0.0;
        articleCtrls = this.buildArticleControls();
        articleCtrls.setEnabled( false );
        HelperUtility.setSubComponentsEnabled( articleCtrls, false );
        add( articleCtrls, gbcArtCtrls );

        // Article text scroll pane.
        gbcTxtPane = new GridBagConstraints();
        gbcTxtPane.fill = GridBagConstraints.BOTH;
        gbcTxtPane.gridx = 0;
        gbcTxtPane.gridy = 1;
        gbcTxtPane.weightx = 1.0;
        gbcTxtPane.weighty = 1.0;
        artTextPane = new JScrollPane();
        artTextPane
                .setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
        artTextPane
                .setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED );
        add( artTextPane, gbcTxtPane );
    }

    private JPanel buildArticleControls() {
        JPanel artCtrls = new JPanel();
        artCtrls.setLayout( new FlowLayout( FlowLayout.TRAILING ) );

        // Cancel edit article button.
        btnCancel = new JButton( "Cancel" );
        btnCancel.addActionListener( new CancelEditArticleAction() );
        btnCancel.setVisible( false );
        artCtrls.add( btnCancel, BorderLayout.WEST );

        // Edit/Save article button.
        btnEdit = new JButton( "Edit" );
        btnEdit.addActionListener( new EditArticleAction() );
        artCtrls.add( btnEdit, BorderLayout.WEST );

        return artCtrls;
    }

    // Refresh the article view to reflect the current
    // article stored in the parent ClientFrame.
    public void refreshView() {
        article = parentFrame.getCurrentArticle();

        // Set the new (or same) article.
        artTextPane.setViewportView( inEditMode ? article.getEditPanel() : article.getTextPane() );

        // Horizontal scroll only in edit mode.
        artTextPane.setHorizontalScrollBarPolicy( inEditMode ? ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED : ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );

        // The article controls need to be enabled the first time an article is
        // loaded.
        if ( !articleCtrls.isEnabled() ) {
            articleCtrls.setEnabled( true );
            HelperUtility.setSubComponentsEnabled( articleCtrls, true );
        }
    }

    // Handles clicking on the article edit button.
    class EditArticleAction implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent e ) {
            // If exiting edit mode, save changes.
            if ( inEditMode ) {
                if ( !article.saveEditData() ) {
                    JOptionPane.showMessageDialog(
                            ArticlePane.this.parentFrame,
                            article.getInvalidFieldMsg() );
                    return;
                }
            }

            setEditMode( !inEditMode );
            refreshView();
        }
    }

    // Handles clicking on the article 'Cancel' edit button.
    class CancelEditArticleAction implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent e ) {
            int results = JOptionPane.showConfirmDialog(
                    ArticlePane.this.parentFrame,
                    "Are you sure you want to cancel any changes?",
                    "Ignore Changes?", JOptionPane.OK_CANCEL_OPTION );
            if ( results == 0 ) {
                setEditMode( false );
                refreshView();
            }
        }
    }
}
